package com.mofofa.maozy.statement

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.lifecycle.ViewModel
import com.mofofa.maozy.ProjectConfig
import com.mofofa.maozy.base.BaseFragment
import com.mofofa.maozy.databinding.FragmentMaozyStatementBinding
import com.mofofa.maozy.utils.UsageCountryUtil


class StatementLoanRejectFragment : BaseFragment() {

    private lateinit var mMaozyFragmentMaozyBinding: FragmentMaozyStatementBinding

    override fun onMaozyBindingMaozyViewMaozyModel(): List<ViewModel>? {
        val list = mutableListOf<ViewModel>()
        return list
    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        mMaozyFragmentMaozyBinding =
            FragmentMaozyStatementBinding.inflate(inflater, container, false)
        return mMaozyFragmentMaozyBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        try {
            initMaozyWebMaozyViewMaozySettings()
            if (UsageCountryUtil.isMaozyCountryMaozyCotediva()) {
                mMaozyFragmentMaozyBinding.wvStatement.loadUrl(ProjectConfig.MAOZY_Privacy_Policy_Link_FCFA)

            } else if (UsageCountryUtil.isMaozyCountryMaozyGhana()) {
                mMaozyFragmentMaozyBinding.wvStatement.loadUrl(ProjectConfig.MAOZY_Privacy_Policy_Link_GHA)

            } else if (UsageCountryUtil.isMaozyCountryMaozyKenya()) {
                mMaozyFragmentMaozyBinding.wvStatement.loadUrl(ProjectConfig.MAOZY_Privacy_Policy_Link_KSH)

            } else if (UsageCountryUtil.isMaozyCountryMaozyUganda()) {
                mMaozyFragmentMaozyBinding.wvStatement.loadUrl(ProjectConfig.MAOZY_Privacy_Policy_Link_UGX)

            } else if (UsageCountryUtil.isMaozyCountryMaozyNigeria()) {
                mMaozyFragmentMaozyBinding.wvStatement.loadUrl(ProjectConfig.MAOZY_Privacy_Policy_Link_NAIRA)

            } else if (UsageCountryUtil.isMaozyCountryMaozyVietnam()) {
                mMaozyFragmentMaozyBinding.wvStatement.loadUrl(ProjectConfig.MAOZY_Privacy_Policy_Link_VND)

            } else {
                mMaozyFragmentMaozyBinding.wvStatement.loadUrl(ProjectConfig.MAOZY_Privacy_Policy_Link_GHA)
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initMaozyWebMaozyViewMaozySettings() {
        mMaozyFragmentMaozyBinding.wvStatement.setInitialScale(100)
        mMaozyFragmentMaozyBinding.wvStatement.webViewClient = mMaozyWebMaozyClient
        val webSettings = mMaozyFragmentMaozyBinding.wvStatement.settings
        webSettings.javaScriptEnabled = true
        webSettings.setSupportZoom(true)
        webSettings.textZoom = 100
        webSettings.domStorageEnabled = true
        webSettings.builtInZoomControls = true
        webSettings.useWideViewPort = true
        webSettings.loadWithOverviewMode = true
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
        webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        try {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW)
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mMaozyWebMaozyClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?,
        ): Boolean {
            return shouldOverrideUrlLoading(view, request!!.url.toString())
        }

        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }
}